package com.share51.novel.common.parse;

import com.share51.novel.common.enums.ProxySiteEnums;
import com.share51.novel.config.http.HttpProxy;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 解析 html 代码
 *
 * @author gb
 */
public class ParseHtml {

    public static List<HttpProxy> parseProxySite(String html, ProxySiteEnums enums) {
        List<HttpProxy> list=Collections.emptyList();
        if (StringUtils.isBlank(html)){
            return list;
        }
        switch (enums) {
            case XI_CI:
                list=parseXiCiSite(html);
                break;
            default:
                break;
        }
        return list;
    }

    private static List<HttpProxy> parseXiCiSite(String html){
        List<HttpProxy> list=new LinkedList<>();
        Document parse = Jsoup.parse(html);
        Element ipList = parse.getElementById("ip_list");
        Elements trs = ipList.getElementsByTag("tr");
        for (Element tr:trs) {
            Elements tds = tr.getElementsByTag("td");
            if (tds.size()>0){
                if (!"高匿".equals(tds.get(4).text())){
                    continue;
                }
                if (!"HTTPS".equals(tds.get(5).text())){
                    continue;
                }
                Date updateTime= parseDate(tds.get(9).text());
                if (if3HourBefore(updateTime)){
                    continue;
                }
                if (ifExceedSecond(tds.get(6).getElementsByTag("div").attr("title"))||
                        ifExceedSecond(tds.get(7).getElementsByTag("div").attr("title"))){
                    continue;
                }
                String ip=tds.get(1).text();
                int port= Integer.parseInt(tds.get(2).text());
                HttpProxy proxy=new HttpProxy(tds.get(1).text(),Integer.parseInt(tds.get(2).text()));
                list.add(proxy);
            }
        }
        return list;
    }

    /**
     * 判断是不是超过一秒
     * @param time
     * @return
     */
    private static boolean ifExceedSecond(String time){
        if (StringUtils.isBlank(time)){
            return true;
        }
        String s = time.replace("秒", "");
        float v = Float.parseFloat(s);
        return v>1;
    }
    /**
     * 判断是三小时之前吗
     * @param date
     * @return
     */
    private static boolean if3HourBefore(Date date){
        long time =System.currentTimeMillis()-date.getTime();
        return time/1000/60/60/3>0;
    }

    private static Date parseDate(String dateStr){
        Date date=new Date();
        if (StringUtils.isBlank(dateStr)){
            return date;
        }
        DateFormat df=new SimpleDateFormat("yy-MM-dd HH:mm");
        try {
            return df.parse(dateStr);
        } catch (ParseException e) {
            return date;
        }
    }
}
